<template>
    <view class="page">


        <view class="items">
            <view class="left">
                <view class="type md4Text lgrey">
                    公司名称
                </view>
                <view class="name  md2Title">
                    <input type="text" v-model="name" placeholder="公司名称请输入" />
                </view>
            </view>

            <!-- <view class="right">
				<image src="../../static/imgs/right.png" mode=""></image>
			</view> -->
        </view>

        <!-- <view class="items" @click="goOnIndustry">
			<view class="left">
				<view class="type md4Text lgrey">
					所在行业请选择
				</view>
				
				<view class="name  md2Title mgrey">
					<input type="text" value="" placeholder="请选择"/>
				</view>
			</view>
			
			<view class="right">
				<image src="../../static/imgs/right.png" mode=""></image>
			</view>
		</view> -->

        <view class="items">
            <view class="left">
                <view class="type md4Text lgrey">
                    在职时间
                </view>

                <view class="name  md2Title" style="display: flex;align-items: center;">
                    <input type="text" :value="start" placeholder="入职时间" :disabled="true" @click="show_start = true;">
                    <view style="width: 80rpx;height: 1px;background-color: #666;margin: 0 60rpx;"></view>
                    <input type="text" :value="end" placeholder="离职时间" :disabled="true" @click="show_end = true;">
                </view>
            </view>
        </view>
        <u-datetime-picker :show="show_start" :value="start || '2021-01'" :minDate="-1577952000000"
            :maxDate="Date.now()" mode="year-month" @confirm="setStart" @cancel="show_start = false;">
        </u-datetime-picker>
        <u-datetime-picker :show="show_end" :value="end && end!=='至今' ? end : '2021-01'" :minDate="-1577952000000"
            :maxDate="Date.now()" mode="year-month" @confirm="setEnd" @cancel="show_end = false;">
        </u-datetime-picker>



        <view class="items" @click="goSetJob">
            <view class="left">
                <view class="type md4Text lgrey">
                    职位名称
                </view>

                <view class="name  md2Title ">
                    <input :value="position" type="text" placeholder="请选择" :disabled="true" />
                </view>
            </view>

            <view class="right">
                <image src="../../static/imgs/right.png" mode=""></image>
            </view>
        </view>

        <view class="items" @click="goWorkContent">
            <view class="left u-flex-1">
                <view class="type md4Text lgrey">
                    工作内容
                </view>

                <view class="name  md2Title">
                    <input :value="content" type="text" placeholder="请输入" :disabled="true" />
                </view>
            </view>

            <view class="right">
                <image src="../../static/imgs/right.png" mode=""></image>
            </view>
        </view>

        <view style="margin-top: 60rpx;">
            <u-button type="primary" @click="save()">确定</u-button>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                name: '',
                start: '',
                end: '至今',
                position: '',
                content: '',
                
                show_start: false,
                show_end: false,
                
                is_resume: false,
                edit_index: -1,
            };
        },
        onLoad() {
            var resume = uni.getStorageSync('resume');
            if(resume) {
                this.is_resume = true;
                var edit_index = uni.getStorageSync('edit_work_index');
                if(!isNaN(edit_index)) {
                    uni.removeStorageSync('edit_work_index');
                    this.edit_index = edit_index;
                    if(resume.workData[edit_index]) {
                        this.name = resume.workData[edit_index].name;
                        this.start = resume.workData[edit_index].start;
                        this.end = resume.workData[edit_index].end;
                        this.position = resume.workData[edit_index].position;
                        this.content = resume.workData[edit_index].content;
                    }
                }
            }
        },
        onShow() {
            var content = uni.getStorageSync('gznr');
            if (content) {
                this.content = content;
                uni.removeStorageSync('gznr');
            }
            var position = uni.getStorageSync('select_position');
            if (position) {
                this.position = position.name;
                uni.removeStorageSync('select_position');
            }
        },

        methods: {
            setStart(e) {
                this.show_start = false;
                this.start = uni.$u.timeFormat(e.value, 'yyyy-mm');
            },
            setEnd(e) {
                this.show_end = false;
                this.end = uni.$u.timeFormat(e.value, 'yyyy-mm');
            },
            save() {
                if(!this.name) {
                    return this._toast('请输入公司名称');
                }
                if(!this.start) {
                    return this._toast('请选择入职时间');
                }
                if(!this.position) {
                    return this._toast('请选择职位');
                }
                if(!this.content) {
                    return this._toast('请输入工作内容');
                }
                if(this.is_resume) {
                    this._ajax({
                        url: '/api/user/updateResume',
                        data: {
                            name: this.name,
                            start: this.start,
                            end: this.end,
                            position: this.position,
                            content: this.content,
                            index: this.edit_index,
                            step: 4
                        },
                        success: res => {
                            if(res.code == 1) {
                                uni.setStorageSync('resume', res.data);
                                this._toBack();
                            }else{
                                this._toast(res.msg);
                            }
                        }
                    }, 1);
                }else {
                    this._ajax({
                        url: '/api/user/workData',
                        data: {
                            name: this.name,
                            start: this.start,
                            end: this.end,
                            position: this.position,
                            content: this.content,
                        },
                        success: res => {
                            if(res.code == 1) {
                                uni.setStorageSync('user', res.data);
                                if(res.data.step == 5) {
                                    this._toView('login/step5');
                                }
                            }else{
                                this._toast(res.msg);
                            }
                        }
                    }, 1);
                }
            },

            changeUnit() {
                uni.navigateTo({
                    url: '/pages/views/mine/unitName'
                })
            },

            goOnIndustry() {
                uni.navigateTo({
                    url: '/pages/views/mine/onIndustry'
                })
            },
            goSetJob() {
                uni.navigateTo({
                    url: '/pages/views/mine/setJob'
                })
            },
            goWorkContent() {
                uni.setStorageSync('gznr', this.content);
                uni.navigateTo({
                    url: '/pages/views/mine/workContent'
                })
            }

        }
    };
</script>

<style scoped lang="less">
    .page {
        padding: 0 32upx;
        background-color: #fff;

        .items {
            width: 100%;
            height: 156upx;
            border-bottom: 2upx solid #e5e5e5;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 20upx;

            .photo {
                image {
                    width: 112upx;
                    height: 112upx;
                }
            }

            .left {
                flex: 1;

                .name {
                    font-weight: bold;
                    margin-top: 30upx;

                }
            }

            .right {
                margin-left: 30rpx;

                image {
                    width: 48upx;
                    height: 48upx;
                }
            }
        }
    }
</style>
